public class Solution {
    public boolean isMatch(String s, String p) {
        int len1 = s.length();
        int len2 = p.length();

        char[] ch1 = s.toCharArray();
        char[] ch2 = p.toCharArray();

        //s的0-i与p的0-j是否匹配
        boolean[][] dp = new boolean[len1 + 1][len2 + 1];

        dp[0][0] = true;
        for (int j = 1; j < len2 + 1; j++) {
            if (ch2[j - 1] == '*') {//?不能匹配空字符串
                dp[0][j] = true;
            } else {
                break;
            }
        }

        for (int i = 1; i < len1 + 1; i++) {
            for (int j = 1; j < len2 + 1; j++) {
                if (ch1[i - 1] == ch2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    if (ch2[j - 1] == '?') {
                        dp[i][j] = dp[i - 1][j - 1];
                    } else if (ch2[j - 1] == '*') {
                        dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                    }
                }
            }
        }

        return dp[len1][len2];
    }
}
